#include <gtest/gtest.h>
#include <iostream>
#include "../mqserver/virtualhost.hpp"

using namespace std;
using namespace ns_rabbitmq;

class HostTest : public testing::Test
{
public:
    void SetUp() override
    {
        auto _map = std::unordered_map<std::string, std::string>();
        std::cout << "初始化工作" << std::endl;
        hostptr = std::make_shared<ns_rabbitmq::VirtualHost>("host1", "./data/host1/message/", "./data/host1/host1.db");
        hostptr->DeclareExchange("exchange1", ExchangeType::DIRECT, true, false, _map);
        hostptr->DeclareExchange("exchange2", ExchangeType::TOPIC, true, true, _map);

        hostptr->DeclareQueue("queue1", true, true, false, _map);
        hostptr->DeclareQueue("queue2", true, true, false, _map);

        hostptr->Bind("exchange1", "queue1", "music.pop.#", true);
        hostptr->Bind("exchange1", "queue2", "music.pop.#", true);
        hostptr->Bind("exchange2", "queue1", "music.pop.#", true);
        hostptr->Bind("exchange2", "queue2", "music.pop.#", true);

        // 推送
        hostptr->BasicPublish("queue1", nullptr, "Hello World-1");
        hostptr->BasicPublish("queue1", nullptr, "Hello World-2");

        hostptr->BasicPublish("queue2", nullptr, "Hello World-1");
        hostptr->BasicPublish("queue2", nullptr, "Hello World-2");
    }

    void TearDown() override
    {
        cout << "清理工作" << endl;
    }

public:
    ns_rabbitmq::HostPtr hostptr;
};

// TEST_F(HostTest, init_insert)
// {
//     ASSERT_EQ(hostptr->IsExchangeExists("exchange1"), true);
//     ASSERT_EQ(hostptr->IsExchangeExists("exchange2"), true);

//     ASSERT_EQ(hostptr->IsQueueExists("queue1"), true);
//     ASSERT_EQ(hostptr->IsQueueExists("queue2"), true);

//     ASSERT_EQ(hostptr->IsBindExists("exchange1", "queue1"), true);
//     ASSERT_EQ(hostptr->IsBindExists("exchange1", "queue2"), true);
//     ASSERT_EQ(hostptr->IsBindExists("exchange2", "queue1"), true);
//     ASSERT_EQ(hostptr->IsBindExists("exchange2", "queue2"), true);

//     MessagePtr msgptr1 = hostptr->BasicConsume("queue1");
//     ASSERT_EQ(msgptr1->payload().body(), string("Hello World-1"));
//     MessagePtr msgptr2 = hostptr->BasicConsume("queue1");
//     ASSERT_EQ(msgptr2->payload().body(), string("Hello World-2"));
//     MessagePtr msgptr3 = hostptr->BasicConsume("queue1");
//     ASSERT_EQ(msgptr3.get(), nullptr);
// }

TEST_F(HostTest, remove_queue)
{
    hostptr->DeleteQueue("queue1");
    ASSERT_EQ(hostptr->IsQueueExists("queue1"), false);
    ASSERT_EQ(hostptr->IsBindExists("exchange1", "queue1"), false);
    MessagePtr msgptr1 = hostptr->BasicConsume("queue1");
    ASSERT_EQ(msgptr1.get(), nullptr);
}

TEST_F(HostTest, exchange)
{
    hostptr->DeleteExchange("exchange1");
    ASSERT_EQ(hostptr->IsExchangeExists("exchange1"), false);
    ASSERT_EQ(hostptr->IsBindExists("exchange1", "queue1"), false);
}

TEST_F(HostTest, ack_message)
{
    MessagePtr msgptr1 = hostptr->BasicConsume("queue1");
    ASSERT_EQ(msgptr1->payload().body(), string("Hello World-1"));
    hostptr->Ack("queue1", msgptr1->payload().basic_properties().id());

}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    RUN_ALL_TESTS();
    return 0;
}
